#include "flash_uid.h"
#include "string.h"
 /*
  * @brief  flash_uid_init存储uidflash的初始化函数
  * @param  None
  * @retval None
  */
void flash_uid_init(void)
{
	FLASH_SetLatency(FLASH_Latency_2);//2周期缓存延迟
	FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//使能flash缓冲存取
	key_uid_init();//flash_rfid_key初始化
}

 /*
  * @brief  key_uid_init判断flash是否需要初始化
  * @param  
  * @retval None
  */
void key_uid_init(void)
{
	uint8_t i,j,buf;
	uint16_t key_buf[3] = {0x0101,0x0101,0x0101};
	
	uint32_t uid_size_buf = 0x00000000;
//判断是否初始化了
	for(i=0;i<RFID_KEY_FLASH_SIZE/FLASH_PAGE_SIZE;i++)
	{
		buf = flash_read_byte(UID_KEY_BASE_ADDRESS+i);
		if(buf > 250)
		{
			for(j=0;j<RFID_KEY_FLASH_SIZE/FLASH_PAGE_SIZE;j++)
			{
				flash_erase_page(UID_KEY_BASE_ADDRESS + j*FLASH_PAGE_SIZE);
			}
			flash_write_4byte(UID_KEY_BASE_ADDRESS,&uid_size_buf,1);
			flash_write_2byte(KEY_ADMIN_FLASH_ADDRESS,key_buf,3);
			for(j=0;j<3;j++)
			{
				key_buf[j] = 0;
				key_buf[j] |=0x0202; 
			}
			flash_write_2byte(KEY_USER_FLASH_ADDRESS,key_buf,3);
			return;
		}
	}
}


 /*
  * @brief  uid_flash_exist判断uid是否存在falsh中
  * @param  uid：uid数据
  * @param page_num：返回的flash页码，从0开始
  * @param uid_flash_num：返回的uid所在位置页码，从0开始
	* @retval 1存在 0不存在
  */
uint8_t uid_flash_exist(uint8_t *uid,uint8_t *page_num,uint8_t *uid_flash_num)
{
	uint8_t i,j;
	uid_flash_buf uid_buf;
	uint8_t uid_num[RFID_KEY_FLASH_SIZE/FLASH_PAGE_SIZE];
	//获取uid数量
	for(i=0;i<RFID_KEY_FLASH_SIZE/FLASH_PAGE_SIZE;i++)
	{
		uid_num[i] = flash_read_byte(UID_KEY_BASE_ADDRESS + i);
		//比对uid
		for(j=0;j<uid_num[i];j++)
		{
			uid_buf.uids = flash_read_4byte(UID_START_ADDRESS + i*FLASH_PAGE_SIZE + j*4);
			//比对成功
			if(comparison_uid(uid,uid_buf.uid))
			{
				*page_num = i;
				*uid_flash_num = j;
				return 1;
			}
		}
	}
	return 0;
}


 /*
  * @brief  comparison_uid比对4字节uid
  * @param  uid1、uid2：比对的uid
	* @retval 1 uid相同 0 uid不相同
  */
uint8_t comparison_uid(uint8_t *uid1,uint8_t *uid2)
{
	uint8_t i;
	for(i=0;i<4;i++)
	{
		if(uid1[i] != uid2[i])
		{
			return 0;
		}
	}
	return 1;
}

 /*
  * @brief  add_uid 加卡
	* @param  uid:要加入的uid数据
  * @retval 0 加入失败 1加入成功
  */
uint8_t add_uid(uint8_t *uid)
{
	uint8_t page_num,uid_num;
	uint16_t i;
	uid_flash_buf uid_buf;
	//获取uid加入的page号和该page已存入的uid数量
	for(i=0;i<RFID_KEY_FLASH_SIZE/FLASH_PAGE_SIZE;i++)
	{
		uid_num = flash_read_byte(UID_KEY_BASE_ADDRESS + i);
		if(uid_num<250)
		{
			page_num = i;
			break;
		}
		else
		{
			uid_num = 255;
		}
	}
	//若是已满，则加入失败
	if(uid_num>250)
	{
		return 0;
	}
	
	if(page_num == 0)//如果是在第一页
	{
		//备份缓冲page
		flash_erase_page(BUFFER_ADDRESS);
		i = 16 + uid_num*4;
		transfer_page_data(UID_KEY_BASE_ADDRESS,BUFFER_ADDRESS,i);//备份
		
		//重新写入uid数量
		flash_erase_page(UID_KEY_BASE_ADDRESS);
		uid_buf.uids = flash_read_4byte(BUFFER_ADDRESS);
		uid_num++; 
		uid_buf.uid[page_num] = uid_num;
		flash_write_4byte(UID_KEY_BASE_ADDRESS,&uid_buf.uids,1);
		
		//写入备份数据
		transfer_page_data(BUFFER_ADDRESS+4,UID_KEY_BASE_ADDRESS+4,(i-4));
		
		//写入新uid
		memcpy(uid_buf.uid,uid,4);
		flash_write_4byte(UID_KEY_BASE_ADDRESS + i ,&uid_buf.uids,1);
	}
	else //非第一页
	{
		//备份缓冲第一页
		flash_erase_page(BUFFER_ADDRESS);
		transfer_page_data(UID_KEY_BASE_ADDRESS,BUFFER_ADDRESS,FLASH_PAGE_SIZE);
		
		//重新写入uid数量
		flash_erase_page(UID_KEY_BASE_ADDRESS);
		uid_buf.uids = flash_read_4byte(BUFFER_ADDRESS);
		i = 16 + uid_num*4;
		uid_num++;
		uid_buf.uid[page_num] = uid_num;
		flash_write_4byte(UID_KEY_BASE_ADDRESS,&uid_buf.uids,1);
		transfer_page_data(BUFFER_ADDRESS+4,UID_KEY_BASE_ADDRESS+4,FLASH_PAGE_SIZE-4);//备份
		
		/*写入新的uid*/
		memcpy(uid_buf.uid,uid,4);
		flash_write_4byte(UID_KEY_BASE_ADDRESS + FLASH_PAGE_SIZE*page_num + i 
		,&uid_buf.uids,1);
		
	}
	return 1;
}

 /*
  * @brief  uid_flash_num 减uid
  * @param  page_num ：要减去的uid的所在页 0开始
  * @param  uid_flash_num ：要减去的uid的索引 0开始
  * @retval 1成功 0失败
  */
uint8_t sub_uid(uint8_t page_num,uint8_t uid_flash_num)
{
	uid_flash_buf uid_buf;
	uint16_t i,j;
	uid_buf.uids = flash_read_4byte(UID_KEY_BASE_ADDRESS);
	if(page_num == 0)//在第一页
	{
		//备份缓冲page
		flash_erase_page(BUFFER_ADDRESS);
		i = 16 + (uid_buf.uid[page_num])*4;
		transfer_page_data(UID_KEY_BASE_ADDRESS,BUFFER_ADDRESS,i);//备份

		//重新写uid
		uid_buf.uid[page_num] = uid_buf.uid[page_num] - 1;
		flash_erase_page(UID_KEY_BASE_ADDRESS);
		flash_write_4byte(UID_KEY_BASE_ADDRESS,&uid_buf.uids,1);
		
		//写第一部分uid
		j = 16 + uid_flash_num*4;
		transfer_page_data(BUFFER_ADDRESS+4,UID_KEY_BASE_ADDRESS+4,j-4);

		//写第二部分uid  j
		transfer_page_data(BUFFER_ADDRESS+j+4,UID_KEY_BASE_ADDRESS+j,i-j-4);
		
	}
	else //非第一页
	{
		//备份缓冲第一页
		flash_erase_page(BUFFER_ADDRESS);
		transfer_page_data(UID_KEY_BASE_ADDRESS,BUFFER_ADDRESS,FLASH_PAGE_SIZE);
		
		//重新写入uid数量
		flash_erase_page(UID_KEY_BASE_ADDRESS);
		uid_buf.uids = flash_read_4byte(BUFFER_ADDRESS);
		i = 16 + (uid_buf.uid[page_num])*4;
		uid_buf.uid[page_num] = uid_buf.uid[page_num] - 1;
		flash_write_4byte(UID_KEY_BASE_ADDRESS,&uid_buf.uids,1);
		transfer_page_data(BUFFER_ADDRESS+4,UID_KEY_BASE_ADDRESS+4,FLASH_PAGE_SIZE-4);//备份
		
		
		//备份uid所在page
		j = 16 + uid_flash_num*4;
		
		flash_erase_page(BUFFER_ADDRESS);
		transfer_page_data(UID_KEY_BASE_ADDRESS+page_num*FLASH_PAGE_SIZE,
		BUFFER_ADDRESS,i);

		//写入第一部分
		flash_erase_page(UID_KEY_BASE_ADDRESS + page_num*FLASH_PAGE_SIZE);
		transfer_page_data(BUFFER_ADDRESS,UID_KEY_BASE_ADDRESS + page_num*FLASH_PAGE_SIZE,j);
		
		//写入第二部分
		transfer_page_data(BUFFER_ADDRESS+j+4,
		UID_KEY_BASE_ADDRESS + page_num*FLASH_PAGE_SIZE+j,i-j-4);
	}
	return 1;
}

//0存储失败
uint8_t storage_wifi_name(uint8_t *name,uint8_t len)
{
  uint8_t i;
  uint16_t buffer;
  if(len<=0 || len > 32)
  {
    return 0;
  }
  //备份
  flash_erase_page(BUFFER_ADDRESS);
  transfer_page_data(WIFINAME_ADDRESS_SIZE,BUFFER_ADDRESS,68);
  
  //存wifi数据
  flash_erase_page(WIFINAME_ADDRESS_SIZE);
  buffer = 0xffff & len;
  flash_write_2byte(WIFINAME_ADDRESS_SIZE,&buffer,1);
  if(len%2!=0)
  {
    name[len] = 0xff;
    for(i=0;i< len+1  ;i+=2)
    {
      buffer = (name[i+1]<<8)|name[i];
      flash_write_2byte(WIFINAME_ADDRESS+i,&buffer,1);
    }
  }
  else
  {
    for(i=0;i< len ;i+=2)
    {
      buffer = (name[i+1]<<8)|name[i];
      flash_write_2byte(WIFINAME_ADDRESS+i,&buffer,1);
    }
  }
  
  transfer_page_data(BUFFER_ADDRESS+34,WIFIPASSWORD_ADDRESS_SIZE,34);
  return 1;
}

//0存储失败
uint8_t storage_wifi_password(uint8_t *password,uint8_t len)
{
  uint8_t i;
  uint16_t buffer;
  if(len<=0 || len > 32)
  {
    return 0;
  }
  //备份
  flash_erase_page(BUFFER_ADDRESS);
  transfer_page_data(WIFINAME_ADDRESS_SIZE,BUFFER_ADDRESS,68);
  
  //存wifi数据
  flash_erase_page(WIFINAME_ADDRESS_SIZE);
  buffer = 0xffff & len;
  flash_write_2byte(WIFIPASSWORD_ADDRESS_SIZE,&buffer,1);
  if(len%2!=0)
  {
    password[len] = 0xff;
    for(i=0;i< len+1  ;i+=2)
    {
      buffer = (password[i+1]<<8)|password[i];
      flash_write_2byte(WIFIPASSWORD_ADDRESS+i,&buffer,1);
    }
  }
  else
  {
    for(i=0;i< len ;i+=2)
    {
      buffer = (password[i+1]<<8)|password[i];
      flash_write_2byte(WIFIPASSWORD_ADDRESS+i,&buffer,1);
    }
  }
  
 transfer_page_data(BUFFER_ADDRESS,WIFINAME_ADDRESS_SIZE,34);
  return 1;
}
